package com.wo.cloud.admin.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wo.cloud.admin.entity.SysMenu;
import com.wo.cloud.admin.service.IMenuService;
import com.wo.cloud.admin.util.SysFeignConstant;
import com.wo.cloud.admin.vo.SysMenuVo;
import com.wo.cloud.core.entity.Response;
import com.wo.cloud.log.annotation.Log;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liry
 * @version 1.0
 * @date Created on 2021/5/19 17:38
 * Description: 菜单管理
 */

@RestController
@RequestMapping(SysFeignConstant.SYS_MENU_BASE_URL)
@Api(tags = "菜单管理模块")
public class SysMenuController {

    @Autowired
    private IMenuService menuService;


    // 新增菜单
    @PostMapping("/save")
    @Log
    public Response create(@RequestBody SysMenu sysMenu) {
        menuService.saveOrUpdate(sysMenu);
        if (StrUtil.isNotBlank(sysMenu.getId())) {
            return Response.success();
        }
        return Response.fail();
    }


    // 删除菜单
    @DeleteMapping("/{id}")
    @Log
    public Response deleteById(@PathVariable("id") String id) {
        boolean removeById = menuService.removeById(id);
        if (removeById) {
            return Response.success();
        }
        return Response.fail();
    }

    // 修改菜单
    @GetMapping("/{id}")
    @Log
    public Response<SysMenu> getById(@PathVariable("id") String id) {
        SysMenu sysMenu = menuService.getById(id);
        if (ObjectUtil.isNotNull(sysMenu)) {
            return Response.fail("该数据不存在");
        }
        return Response.data(sysMenu);
    }

    // 分页条件查询
    @GetMapping("/page")
    @Log
    public Response<IPage<SysMenu>> page(Page page, @RequestParam(required = false) String name) {
        IPage<SysMenu> sysMenuIPage = menuService.selectPage(page, name);
        return Response.data(sysMenuIPage);
    }

    // 分页列表
    @GetMapping("/list")
    @Log
    public Response<List<SysMenu>> list() {
        List<SysMenu> list = menuService.list();
        return Response.data(list);
    }

    // 获取树形菜单
    @GetMapping("/treeMenu")
    @Log
    public Response<List<SysMenuVo>> getTreeMenu(@RequestParam(value = "roleId") String roleId){
        List<SysMenuVo> sysMenuVos = menuService.getTreeMenu(roleId);
        return Response.data(sysMenuVos);
    }

}
